package com.ship.dispatch.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <b>DateErgodicUtil</b>
 *
 * @description: DateErgodicUtil <br>
 * @date: 2024/6/2 09:38 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */
public class DateErgodicUtil {

    public static List<String> dateErgodicUtil(String startDate, String endDate){
        List<String> returnList = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startTime = LocalDate.parse(startDate, formatter);
        LocalDate endTime = LocalDate.parse(endDate, formatter);
        int number = (int) ChronoUnit.MONTHS.between(startTime,endTime);
        for (int i = 0; i <= number; i++) {
            LocalDate middleDate = startTime.plusMonths(i);
            ZonedDateTime zonedDateTime = middleDate.atStartOfDay(ZoneId.systemDefault());
            // 转换为Instant
            Instant instant = zonedDateTime.toInstant();
            // 转换为java.util.Date
            Date date = Date.from(instant);
            returnList.add(DateUtil.format(date, DatePattern.SIMPLE_MONTH_FORMATTER));
        }
        return returnList;
    }
}
